'''
Author: xubing
Date: 2021-07-21 16:46:47
LastEditors: xubing
LastEditTime: 2021-08-02 15:28:12
FilePath: /my_gists/从hive获取多个table的信息并写入到一个excel文件里.py
'''
'''
Author: xubing
Date: 2021-07-21 16:46:47
LastEditors: xubing
LastEditTime: 2021-07-21 16:54:02
FilePath: /my_gists/从hive获取多个table的信息并写入到一个excel文件里.py
'''
from pyhive import hive
import pandas as pd
from impala.util import as_pandas  # 这个函数可以直接cur.execute()的内容转换成dataframe，超级方便
conn = hive.Connection(
    host='10.32.36.143',
    port=10000,
    username='xubing'
)


def get_table_info(table_name: list, output_excel):
    dfs = []
    cur = conn.cursor()
    for t in table_name:
        cur.execute('desc %s' % t)
        df = as_pandas(cur)
        dfs.append(df)

    with pd.ExcelWriter(output_excel) as writer:
        for df, t in zip(dfs, table_name):
            df.to_excel(writer, sheet_name=t, index=False, encoding='utf8')
